home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TUTOROOT.PAK / STEP06.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  209 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1994 by Borland International
  3. //   Tutorial application -- step06.cpp
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/dc.h>
  9. #include <owl/inputdia.h>
  10. #include <classlib/arrays.h>
  11. #include <stdlib.h>
  12. #include "step06.rc"
  13.  
  14. typedef TArray<TPoint> TPoints;
  15. typedef TArrayIterator<TPoint> TPointsIterator;
  16.  
  17. class TDrawWindow : public TWindow {
  18.   public:
  19.     TDrawWindow(TWindow* parent = 0);
  20.    ~TDrawWindow()
  21.     {
  22.       delete DragDC;
  23.       delete Pen;
  24.       delete Line;
  25.     }
  26.  
  27.     void SetPenSize(int newSize);
  28.  
  29.   protected:
  30.     TDC* DragDC;
  31.     int PenSize;
  32.     TPen* Pen;
  33.     TPoints* Line; // To store points in line.
  34.  
  35.     // Override member function of TWindow
  36.     bool CanClose();
  37.  
  38.     // Message response functions
  39.     void EvLButtonDown(uint, TPoint&);
  40.     void EvRButtonDown(uint, TPoint&);
  41.     void EvMouseMove(uint, TPoint&);
  42.     void EvLButtonUp(uint, TPoint&);
  43.     void Paint(TDC&, bool, TRect&);
  44.     void CmFileNew();
  45.     void CmFileOpen();
  46.     void CmFileSave();
  47.     void CmFileSaveAs();
  48.     void CmAbout();
  49.  
  50.   DECLARE_RESPONSE_TABLE(TDrawWindow);
  51. };
  52.  
  53. DEFINE_RESPONSE_TABLE1(TDrawWindow, TWindow)
  54.   EV_WM_LBUTTONDOWN,
  55.   EV_WM_RBUTTONDOWN,
  56.   EV_WM_MOUSEMOVE,
  57.   EV_WM_LBUTTONUP,
  58.   EV_COMMAND(CM_FILENEW, CmFileNew),
  59.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  60.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  61.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  62.   EV_COMMAND(CM_ABOUT, CmAbout),
  63. END_RESPONSE_TABLE;
  64.  
  65. TDrawWindow::TDrawWindow(TWindow* parent)
  66. {
  67.   Init(parent, 0, 0);
  68.   DragDC  = 0;
  69.   PenSize = 1;
  70.   Pen     = new TPen(TColor::Black, PenSize);
  71.   Line    = new TPoints(10, 0, 10);
  72. }
  73.  
  74. bool
  75. TDrawWindow::CanClose()
  76. {
  77.   return MessageBox("Do you want to save?", "Drawing has changed",
  78.                     MB_YESNO | MB_ICONQUESTION) == IDNO;
  79. }
  80.  
  81. void
  82. TDrawWindow::EvLButtonDown(uint, TPoint& point)
  83. {
  84.   Line->Flush();
  85.   Invalidate();
  86.  
  87.   if (!DragDC) {
  88.     SetCapture();
  89.     DragDC = new TClientDC(*this);
  90.     DragDC->SelectObject(*Pen);
  91.     DragDC->MoveTo(point);
  92.     Line->Add(point);
  93.   }
  94. }
  95.  
  96. void
  97. TDrawWindow::EvRButtonDown(uint, TPoint&)
  98. {
  99.   char inputText[6];
  100.  
  101.   wsprintf(inputText, "%d", PenSize);
  102.   if ((TInputDialog(this, "Line Thickness",
  103.                         "Input a new thickness:",
  104.                         inputText,
  105.                         sizeof(inputText))).Execute() == IDOK) {
  106.     int newPenSize = atoi(inputText);
  107.  
  108.     if (newPenSize < 0)
  109.       newPenSize = 1;
  110.  
  111.     SetPenSize(newPenSize);
  112.   }
  113. }
  114.  
  115. void
  116. TDrawWindow::EvMouseMove(uint, TPoint& point)
  117. {
  118.   if (DragDC) {
  119.     DragDC->LineTo(point);
  120.     Line->Add(point);
  121.   }
  122. }
  123.  
  124. void
  125. TDrawWindow::EvLButtonUp(uint, TPoint&)
  126. {
  127.   if (DragDC) {
  128.     ReleaseCapture();
  129.     delete DragDC;
  130.     DragDC = 0;
  131.   }
  132. }
  133.  
  134. void
  135. TDrawWindow::SetPenSize(int newSize)
  136. {
  137.   delete Pen;
  138.   PenSize = newSize;
  139.   Pen     = new TPen(TColor::Black, PenSize);
  140. }
  141.  
  142. void
  143. TDrawWindow::Paint(TDC& dc, bool, TRect&)
  144. {
  145.   bool first = true;
  146.   TPointsIterator i(*Line);
  147.  
  148.   dc.SelectObject(*Pen);
  149.  
  150.   while (i) {
  151.     TPoint p = i++;
  152.  
  153.     if (!first)
  154.       dc.LineTo(p);
  155.     else {
  156.       dc.MoveTo(p);
  157.       first = false;
  158.     }
  159.   }
  160. }
  161.  
  162. void
  163. TDrawWindow::CmFileNew()
  164. {
  165.   Line->Flush();
  166.   Invalidate();
  167. }
  168.  
  169. void
  170. TDrawWindow::CmFileOpen()
  171. {
  172.   MessageBox("Feature not implemented", "File Open", MB_OK);
  173. }
  174.  
  175. void
  176. TDrawWindow::CmFileSave()
  177. {
  178.   MessageBox("Feature not implemented", "File Save", MB_OK);
  179. }
  180.  
  181. void
  182. TDrawWindow::CmFileSaveAs()
  183. {
  184.   MessageBox("Feature not implemented", "File Save As", MB_OK);
  185. }
  186.  
  187. void
  188. TDrawWindow::CmAbout()
  189. {
  190.   MessageBox("Feature not implemented", "About Drawing Pad", MB_OK);
  191. }
  192.  
  193. class TDrawApp : public TApplication {
  194.   public:
  195.     TDrawApp() : TApplication() {}
  196.  
  197.     void InitMainWindow()
  198.     {
  199.       SetMainWindow(new TFrameWindow(0, "Drawing Pad", new TDrawWindow));
  200.       GetMainWindow()->AssignMenu("COMMANDS");
  201.     }
  202. };
  203.  
  204. int
  205. OwlMain(int /*argc*/, char* /*argv*/ [])
  206. {
  207.   return TDrawApp().Run();
  208. }
  209.